package com.lx.chong.utils.pageable;


import com.lx.chong.dto.QueryDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: whh
 * @date: 2018/1/26
 */
@Component
public class QueryDto2MyQuery {

    public List<MyQuery> parse(List<QueryDto> dtos) {
        List<MyQuery> queries = new ArrayList<>();
        for (QueryDto dto : dtos) {
            MyQuery query = new MyQuery();
            BeanUtils.copyProperties(dto, query);
            query.setType(getEnumFromString(dto.getType()));
            queries.add(query);
        }
        return queries;
    }

    public MyQuery parse(QueryDto dto) {
        MyQuery query = new MyQuery();
        BeanUtils.copyProperties(dto, query);
        query.setType(getEnumFromString(dto.getType()));
        return query;
    }

    /**
     * 通过字符串获取MyQueryType枚举对象
     * @param key
     * @return
     */
    public MyQueryType getEnumFromString(String key) {
        for (MyQueryType type : MyQueryType.values()) {
            if(type.name().equals(key)) {
                return type;
            }
        }
        return null;
    }
}
